package com.telnetnz.ticketdesk.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.ehcache.Element;

import org.apache.log4j.Logger;

import com.telnetnz.ticketdesk.bean.Project;
import com.telnetnz.ticketdesk.common.config.SpringContext;
import com.telnetnz.ticketdesk.dao.entity.ProjectEntity;
import com.telnetnz.ticketdesk.dao.wrapper.ProjectWrap;

public class ProjectService
{
	public static final String[] ORDER_BY = ProjectWrap.ORDER_BY;
	public static final int ORDERBY_NAME = 0;

	public static final String[] ORDER = ProjectWrap.ORDER;

	private static Logger logger = Logger.getLogger(ProjectService.class);
	private static String CACHE_KEY = "id_";

	/**
	 * Important: This is only for comparison with the updated bean
	 * 
	 * @param uid
	 * @return
	 */
	public static Project getRawProject(int id)
	{
		ProjectEntity entity = getWrap().select(id);
		return Project.adapt(entity);
	}

	/**
	 * Important: This is the only entry to get the project bean, including the methods that return instances list
	 * 
	 * @param uid
	 * @return
	 */
	public static Project getProject(int id)
	{
		Element elm = getWrap().getCache().get(CACHE_KEY + id);
		Project bean;
		if (elm == null)
		{
			ProjectEntity entity = getWrap().select(id);
			bean = Project.adapt(entity);
			getWrap().getCache().put(new Element(CACHE_KEY + id, bean));
		}
		else
		{
			bean = (Project) elm.getValue();
		}
		return bean;
	}

	public static void add(Project project)
	{
		if (project == null)
			return;
		getWrap().insert(project.toEntity());
		getWrap().getCache().remove(CACHE_KEY + project.getId());
	}

	public static void update(Project project)
	{
		if (project == null)
			return;
		project.setUpdateTime(new Date());
		getWrap().update(project.toEntity());
		getWrap().getCache().remove(CACHE_KEY + project.getId());
	}

	public static void delete(Project project, Project target)
	{
		if (project == null)
			return;
		ProjectToUserService.deleteByProjectId(project.getId());
		getWrap().delete(project.toEntity());

		if (target != null)
			TicketService.updateBatchToProjectId(project.getId(), target.getId());

		getWrap().getCache().remove(CACHE_KEY + project.getId());
	}

	public static List<Project> getProjects(List<Integer> ids)
	{
		return adaptIdsList(ids);
	}

	public static List<Project> getByPageAndCondition(
			int start,
			int limit, 
			int orderby, 
			int order,
			List<Integer> excludedIds)
	{
		List<Integer> ids = getWrap().selectByPageAndCondition(start, limit, orderby, order, excludedIds);
		return adaptIdsList(ids);
	}

	public static int countByCondition(List<Integer> excludedIds)
	{
		return getWrap().countByCondition(excludedIds);
	}

	private static List<Project> adaptIdsList(List<Integer> ids)
	{
		List<Project> projects = new ArrayList<Project>();
		if (ids != null && ids.size() > 0)
		{
			for (int id : ids)
			{
				Project project = getProject(id);
				if (project != null)
					projects.add(project);
			}
		}
		return projects;
	}

	private static ProjectWrap getWrap()
	{
		return SpringContext.getProjectWrap();
	}

	public static void main(String[] args)
	{
		
	}
}
